﻿/*
=======================================================
    StandStatusDetail【状态标准明细】
=======================================================
*/

namespace Services
{
    public interface IStandStatusDetailLogic : ILogic<StandStatusDetailModel, StandStatusDetailFilterModel>
    {
    }


    public class StandStatusDetailLogic : BaseLogic<StandStatusDetailModel, StandStatusDetailFilterModel, StandStatusDetail, IStandStatusDetailRepository, IStandStatusDetailMapping, IStandStatusDetailService>, IStandStatusDetailLogic
    {
        private readonly IUsingWireropeRepository _IUsingWireropeRepository;
        private readonly IJudgeWireropeStatusMonitor _IJudgeWireropeStatusMonitor;
        private readonly IStandStatusRepository _StandStatusRepository;

        public StandStatusDetailLogic(ICurrentUser currentUser, IStandStatusDetailRepository repository, IStandStatusDetailMapping mapping, IStandStatusDetailService service, IUsingWireropeRepository iUsingWireropeRepository, IJudgeWireropeStatusMonitor iJudgeWireropeStatusMonitor, IStandStatusRepository standStatusRepository)
            : base(currentUser, repository, mapping, service)
        {
            _IUsingWireropeRepository = iUsingWireropeRepository;
            _IJudgeWireropeStatusMonitor = iJudgeWireropeStatusMonitor;
            _StandStatusRepository = standStatusRepository;
        }

        #region BatchUpdate
        public override int BatchUpdate(List<StandStatusDetailModel> models)
        {
            //得到原本数据库中的数据
            var where = Expressionable.Create<StandStatusDetail>();
            where = where.And(x => models.Select(x => x.StandStatusDetailID).ToList().Contains(x.StandStatusDetailID));
            var originDbData = _repository.GetSearch(where);

            var standStatus = _StandStatusRepository.GetByID(models.First().StandStatusID);
            standStatus.Remark = models.First().StandStatus_Remark;

            _StandStatusRepository.Update(standStatus);

            //更新数据
            int n = base.BatchUpdate(models);

            foreach (var dbItem in originDbData)
            {
                var webItem = models.First(x => x.StandStatusDetailID == dbItem.StandStatusDetailID);

                if (dbItem.GreenLight == webItem.GreenLight && dbItem.BlueLight == webItem.BlueLight && dbItem.YellowLight == webItem.YellowLight && dbItem.OrangeLight == webItem.OrangeLight)
                {
                    continue;
                }

                var uw_where = Expressionable.Create<UsingWirerope>();
                uw_where = uw_where.And(x => x.HoisterID == dbItem.HoisterID && x.HoisterUsageID == dbItem.HoisterUsageID && x.WireropeTypeID == dbItem.WireropeTypeID);
                var usingWireropes = _IUsingWireropeRepository.GetSearch(uw_where);

                foreach (var uwItem in usingWireropes)
                {
                    var statusMonitor = _IJudgeWireropeStatusMonitor.JudgeWireropeMonitor(uwItem.UsingWireropeID);
                    _IJudgeWireropeStatusMonitor.InsertOrUpdateWireropeMonitor(statusMonitor);
                }
            }

            return n;
        }
        #endregion
    }
}
